package com.zhajh;

import java.util.UUID;

import com.zhajh.utils.Const;
import com.zhajh.utils.ToastUtil;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyApp extends Application
{
    private static MyApp sInstance;

    public static boolean sUserLogined = false;
    public static String sUserName = null;
    public static String sUserPasswd = null;

    @Override
    public void onCreate() {
        super.onCreate();

        sInstance = this;
        readUserInfo();
    }

    @Override
    public void onTerminate()
    {
        super.onTerminate();
    }

    public static MyApp getInstance(){
        return sInstance;
    }

    /**
     * 检查网络状态
     * @return
     */
    public static boolean isNetworkConnected(){
        ConnectivityManager cm = (ConnectivityManager)sInstance.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();

        if(ni != null && ni.isConnected()){
            return true;
        }

        return false;
    }

    //判断当前网络是否为wifi
    public static boolean isWifi()
    {
        ConnectivityManager cm = (ConnectivityManager)sInstance.getSystemService(Context.CONNECTIVITY_SERVICE);
 	    NetworkInfo ni = cm.getActiveNetworkInfo();

 	    if (ni != null
 	        && ni.getType()==ConnectivityManager.TYPE_WIFI)
 	    {
            return true;
     	}

        return false;
    }

    public static void saveUserInfo(String username, String passwd) {

        SharedPreferences share = sInstance.getSharedPreferences(Const.SHARE_SETTINGS, 0);
        SharedPreferences.Editor editor = share.edit();

        editor.putString(Const.SHARE_USERNAME, username);
        editor.putString(Const.SHARE_PASSWORD, passwd);
        editor.commit();
        share = null;

        MyApp.sUserName = username;
        MyApp.sUserPasswd = passwd;
        MyApp.sUserLogined = true;
    }

    public static void readUserInfo()
    {
    	SharedPreferences share = sInstance.getSharedPreferences(Const.SHARE_SETTINGS, 0);
    	MyApp.sUserName = share.getString(Const.SHARE_USERNAME, "");
    	MyApp.sUserPasswd = share.getString(Const.SHARE_PASSWORD, "");
    }

    public static boolean checkLoginStatus(boolean bShowMsg)
    {
        if (!MyApp.isNetworkConnected())
        {
            if (bShowMsg)
            {
                ToastUtil.showMessage(sInstance, R.string.check_network);
            }

            return false;
        }
        else if (sUserName.equals(Const.UNKNOWN_USER))
        {
            if (bShowMsg)
            {
                ToastUtil.showMessage(sInstance, R.string.not_logon);
            }

            return false;
        }
        else if (!sUserLogined)
        {
            if (bShowMsg)
            {
                ToastUtil.showMessage(sInstance, R.string.not_logon);
            }

            return false;
        }

        return true;
    }

    private static int sVersionCode = 0;

    public static int getVersionCode() {
        if (sVersionCode != 0)
        {
            return sVersionCode;
        }

    	Context context = sInstance;
        int verCode = -1;
        String packName = context.getPackageName();

        try {
            verCode = context.getPackageManager().getPackageInfo(
            		packName, 0).versionCode;
        } catch (NameNotFoundException e) {
            Log.e(Const.LOG_TAG, e.getMessage());
        }

        sVersionCode = verCode;
        return sVersionCode;
    }

    private static String sVersionName = null;

    public static String getVersionName() {
        if (sVersionName != null)
        {
            return sVersionName;
        }

    	Context context = sInstance;
    	String packName = context.getPackageName();
        String verName = "";
        try {
            verName = context.getPackageManager().getPackageInfo(
            		packName, 0).versionName;
        } catch (NameNotFoundException e) {
            Log.e(Const.LOG_TAG, e.getMessage());
        }

        sVersionName = verName;

        return sVersionName;
    }

    private static String sDeviceID = null;

    public static String getAndroidDeviceID()
    {
    	if (sDeviceID == null)
    	{
	        final TelephonyManager tm = (TelephonyManager)sInstance.getSystemService(Context.TELEPHONY_SERVICE);

	        final String tmDevice = "" + tm.getDeviceId();
	        final String tmSerial = "" + tm.getSimSerialNumber();
	        final String androidId = "" + android.provider.Settings.Secure.getString(sInstance.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

	        UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());

	        sDeviceID = "Android:"+deviceUuid.toString();
    	}

        return sDeviceID;
    }
}
